﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Client
{
    public partial class FormAuctionClient : Form
    {
        private Client client;
        private int auctionID = 0, coloring = 0;

        public FormAuctionClient()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Sends message to the form with a given backgroud color
        /// </summary>
        /// <param name="msg">The message</param>
        /// <param name="color">The background color</param>
        /// <param name="coloring">The element in the listview to retrieve the same background color</param>
        public void FormMessage(string msg, Color color, int coloring)
        {
            if (InvokeRequired)
            {
                this.Invoke(new Client.FormMessageDelegate(FormMessage), msg, color, coloring);
            }
            else
            {
                lblInfo.Text = msg;
                lblInfo.BackColor = color;
                if (coloring != 0)
                {
                    this.coloring = coloring;
                    foreach (ListViewItem item in lstAuctions.Items)
                    {
                        if (Convert.ToInt32(item.Tag) == coloring)
                        {
                            item.BackColor = color;
                        }
                    }
                }
                if (client.FindAuction(auctionID) == null || client.FindAuction(auctionID).HammerTime == 3)
                {
                    auctionID = 0;
                    btnBid.Enabled = false;
                }
                else
                {
                    btnBid.Enabled = true;
                }
            }
        }

        /// <summary>
        /// Closes the Application
        /// </summary>
        public void CloseApplication()
        {
            if(client != null)
                client.CloseApplication();
        }

        /// <summary>
        /// Updates the listview with current items and their data
        /// </summary>
        public void UpdateAuctions()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Client.FormUpdateAuctions(UpdateAuctions));
            }
            else
            {
                lstAuctions.Items.Clear();
                List<Auction> auctions = client.GetAuctionList();
                foreach (Auction currentAuction in auctions)
                {
                    ListViewItem line = new ListViewItem();
                    line.Text = currentAuction.Name;
                    line.SubItems.Add(currentAuction.Bid.ToString());
                    line.SubItems.Add(currentAuction.HammerTime.ToString());
                    line.Tag = currentAuction.Id;
                    lstAuctions.Items.Add(line);
                }
                if (auctionID != 0)
                    UpdateCurrentAuction();
            }
        }

        /// <summary>
        /// Updates the currently selected auctions visible data
        /// </summary>
        private void UpdateCurrentAuction()
        {
            Auction currentAuction = client.FindAuction(auctionID);
            if (currentAuction != null)
            {
                lblName.Text = currentAuction.Name;
                lblMinimumPrice.Text = currentAuction.MinimumBid.ToString();
                lblBid.Text = currentAuction.Bid.ToString();
                lblTimeLeft.Text = currentAuction.HammerTime.ToString();
                lblWinner.Text = currentAuction.Winner;
            }
        }

        /// <summary>
        /// Creates the client when loading the Form
        /// </summary>
        private void FormAuktionsKlient_Load(object sender, EventArgs e)
        {
            client = new Client("127.0.0.1", 1337, this);
        }

        /// <summary>
        /// Closes the Form/Application
        /// </summary>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// When selecting an item in the listview, then shows more information about the item
        /// </summary>
        private void lstAuctions_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListViewItem currentItem = lstAuctions.FocusedItem;
            if (currentItem != null)
            {
                auctionID = int.Parse(currentItem.Tag.ToString());
                Auction currentAuction = client.FindAuction(auctionID);
                if (currentAuction != null)
                {
                    if (currentAuction.Winner == null)
                        btnBid.Enabled = true;
                    else
                        btnBid.Enabled = false;
                    UpdateCurrentAuction();
                }
            }
        }

        /// <summary>
        /// Sends a bid for the currently selected item
        /// </summary>
        private void btnBid_Click(object sender, EventArgs e)
        {
            int bid;
            if (int.TryParse(txtBid.Text, out bid))
            {
                client.AuctionBid(auctionID, bid);
            }
            else
            {
                MessageBox.Show("Budet er ikke et tal", "Fejl", MessageBoxButtons.OK);
            }
        }

        /// <summary>
        /// Closes the application
        /// </summary>
        private void FormAuctionClient_FormClosing(object sender, FormClosingEventArgs e)
        {
            CloseApplication();
        }
    }
}
